import type { Profile } from '@/types/user'
import { getLocalToken, removeLocalToken, setLocalToken } from '@/utils/auth'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('hm-user', () => {
  // 初始化时，本地若有token就获取本地的token,没有在设置默认值
  const token = ref(getLocalToken() || '')
  const setToken = (val:string)=>{
    token.value = val
    // 使用cookie持久化存储
    setLocalToken(val)
  }
  const clearToken = ()=>{
    token.value = ''
    // 使用cookie清除持久化token
    removeLocalToken()
  }
  // 用户信息
  const profile = ref<Profile>({} as Profile)
  const setProfile = (val:Profile)=>{
    profile.value = val
  }
  const clearProfile = ()=>{
    profile.value = {} as Profile
  }
  return {
    token,
    setToken,
    clearToken,
    profile,
    setProfile,
    clearProfile,
  }
})
